package com.jing.Recyclerview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jing.Recyclerview.R;
import com.jing.Recyclerview.entity.Student;

import java.util.List;

public class FruitRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private Context context;
    private List<Student> Students;
    public FruitRecyclerAdapter(Context context,List<Student> Students){
        this.context=context;
        this.Students=Students;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.layout_student_list, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.name.setText(Students.get(position).getName());
        holder.age.setText(String.valueOf(Students.get(position).getAge()));
        holder.sex.setText(Students.get(position).getSex());

//        holder.layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, Students.get(position).getName(), Toast.LENGTH_SHORT).show();
//            }
//        });


//        holder.bind(Students.get(position), position, new MyViewHolder.OnItemClickListener() {
//            @Override
//            public void onItemClick(int position) {
//                Toast.makeText(context,Students.get(position).getName(), Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return Students.size();
    }
}
class MyViewHolder extends RecyclerView.ViewHolder{
    TextView name;
    TextView age;
    TextView sex;
//    LinearLayout layout;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);
        age = itemView.findViewById(R.id.age);
        sex = itemView.findViewById(R.id.sex);
//        layout = itemView.findViewById(R.id.layout);
    }
//    public void bind(Student student, int position, OnItemClickListener listener) {
//        // 设置数据
//        name.setText(student.getName());
//        age.setText(String.valueOf(student.getAge()));
//        sex.setText(student.getSex());
//
//        // 设置点击事件
//        itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null) {
//                    listener.onItemClick(position);
//                }
//            }
//        });
//    }
//
//    // 定义一个接口用于回调点击事件
//    public interface OnItemClickListener {
//        void onItemClick(int position);
//    }
}